﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Intelligent.OTC.Domain.DataModel;
using Intelligent.OTC.Domain.Repositories;
using Intelligent.OTC.Common.Exceptions;
using System.Data.Entity;
using Intelligent.OTC.Common;

namespace Intelligent.OTC.Business
{
    public class XcceleratorService
    {
        public XcceleratorRepository XRep { private get; set; }

        public T_USER_EMPLOYEE GetUserOrganization(string userId)
        {
            var empId = (from u in XRep.GetDbSet<T_USERS>() 
                       where u.USER_CODE == userId
                        select u.USER_EMPLOYEE_ID).FirstOrDefault();

            if (empId != null)
            {
                var userEmp = XRep.GetDbSet<T_USER_EMPLOYEE>()
                    .Include<T_USER_EMPLOYEE, T_ORG_CENTER>(e => e.T_ORG_CENTER)
                    .Include<T_USER_EMPLOYEE, T_ORG_DEAL>(e => e.T_ORG_DEAL)
                    .Include<T_USER_EMPLOYEE, T_ORG_GROUP>(e => e.T_ORG_GROUP)
                    .Include<T_USER_EMPLOYEE, T_ORG_REGION>(e => e.T_ORG_REGION)
                    .Include<T_USER_EMPLOYEE, T_ORG_TEAM>(e => e.T_ORG_TEAM)
                    .FirstOrDefault(ue => ue.ID == empId);
                if (userEmp != null)
                {
                    return userEmp;
                }
                else
                {
                    throw new OTCServiceException(string.Format("Xccelerator: User employee is not exist for user code: [{0}] employee: [{1}]", userId, empId));
                }
            }
            else
            {
                throw new OTCServiceException(string.Format("Xccelerator: User not exist or user employee is not setup for user code: [{0}]", userId));
            }
        }

        public List<SysUser> GetUsers(int regionId, int centerId, int groupId, int dealId, int teamId)
        {
            return XRep.GetDbSet<T_USERS>().Where(e =>
                e.T_USER_EMPLOYEE.REGION_ID == regionId
                && e.T_USER_EMPLOYEE.CENTER_ID == centerId
                && e.T_USER_EMPLOYEE.GROUP_ID == groupId
                && e.T_USER_EMPLOYEE.DEAL_ID == dealId
                && e.T_USER_EMPLOYEE.TEAM_ID == teamId).Select(u => new SysUser() {
                    EID = u.USER_CODE,
                    Name = u.T_USER_EMPLOYEE.USER_NAME,
                    Email = u.T_USER_EMPLOYEE.USER_MAIL,
                    Deal = u.DEAL,
                    DealId = u.T_USER_EMPLOYEE.DEAL_ID.ToString(),
                    //add by jiaxing
                    //Group=u.T_USER_EMPLOYEE.T_ORG_GROUP.ToString(),
                    GroupId=u.T_USER_EMPLOYEE.GROUP_ID.ToString(),
                    RegionId=u.T_USER_EMPLOYEE.REGION_ID.ToString(),
                    CenterId =u.T_USER_EMPLOYEE.CENTER_ID.ToString(),
                    TeamId=u.T_USER_EMPLOYEE.TEAM_ID.ToString()
                }).ToList();
        }

        /// <summary>
        /// Get all users from collection team.
        /// This method only used by global mail retrival timer.
        /// </summary>
        /// <returns>All collection teams users in all Deal</returns>
        public List<SysUser> GetCollectionTeamMember()
        {
            return XRep.GetDbSet<T_USERS>().Where(e => e.T_USER_EMPLOYEE.T_ORG_TEAM.TEAM_NAME == "Collection" && e.DEAL == "Avery")
                .Select(u => new SysUser()
                {
                    EID = u.USER_CODE,
                    Name = u.T_USER_EMPLOYEE.USER_NAME,
                    Email = u.T_USER_EMPLOYEE.USER_MAIL,
                    Deal = u.DEAL,
                    DealId = u.T_USER_EMPLOYEE.DEAL_ID.ToString()
                }).ToList();
        }

    }
}

